<template>
  <center>
    <div style="margin-top: 120px; width: 400px;">
      <el-form :model="form" :rules="rules" ref="form" label-width="80px">
        <el-form-item label="Username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="Password">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="login">Login</el-button>
    </div>
  </center>
</template>

<script>

export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: 'Please input your username!', trigger: 'blur' }],
        password: [{ required: true, message: 'Please input your password!', trigger: 'blur' }]
      }
    }
  },
  methods: {
    login() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$store.dispatch('login', this.form)
            .then(() => {
              this.$router.push({ path: '/' })
            })
            .catch(() => {
            })
        }
      })
    }
  }
}
</script>

<style></style>